package jesss.commands;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.services.s3.AmazonS3;

/**
 * User: Lee
 * Date: 8/16/13
 * Time: 11:29 AM
 */
public abstract class AbstractJesssCommand<T> implements JesssCommand {

    private AmazonS3 amazonS3;
    public void setAmazonS3(AmazonS3 amazonS3) {
        if (amazonS3 == null) {
            throw new IllegalArgumentException("AmazonS3 object reference not set.");
        }

        this.amazonS3 = amazonS3;
    }

    private AWSCredentials awsCredentials;
    public void setAwsCredentials(AWSCredentials awsCredentials) {
        if (awsCredentials == null) {
            throw new IllegalArgumentException("AWSCredentials object reference not set.");
        }

        this.awsCredentials = awsCredentials;
    }


    private boolean shouldDisplayVerbose = false;
    public boolean getShouldDisplayVerbose() { return shouldDisplayVerbose; }
    public void setShouldDisplayVerbose(boolean shouldDisplayVerbose) { this.shouldDisplayVerbose = shouldDisplayVerbose; }

    protected void displayMessage(String message) {
        if (getShouldDisplayVerbose()) {
            System.out.println(message);
        }
    }

    public void addExceptionInfoToCommandResult (CommandResult<T> commandResult, Exception e) {
        String statusMessage = commandResult.getStatusMessage();

        StackTraceElement[] stackTraceElements = e.getStackTrace();
        StringBuffer stackTrace = new StringBuffer();
        for (int i = 0; i < stackTraceElements.length; i++) {
            stackTrace.append(stackTraceElements[i]).append("\n");
        }
        commandResult.setStatusMessage(String.format("%s : %s%n%n%s", statusMessage, e.getMessage(), stackTrace.toString()));
    }

    @Override
    public abstract CommandResult<T> executeCommand();
}
